package hankz.spring.context.support;

import hankz.spring.beans.BeansException;
import hankz.spring.beans.factory.config.BeanPostProcessor;
import hankz.spring.context.ApplicationContexrAware;
import hankz.spring.context.ApplicationContext;

/**
 * @auther zzc
 * @date 2021 11 23
 * <p>
 * 包装处理类
 */
public class ApplicationContextAwareProcess implements BeanPostProcessor {

    private ApplicationContext applicationContext;

    public ApplicationContextAwareProcess(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ApplicationContexrAware) {
            ((ApplicationContexrAware) bean).setApplicationContext(applicationContext);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
